package com.adcc.loadsheet.billing.controller;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.billing.configuration.SystemConfig;
import com.adcc.loadsheet.billing.service.BillingDataMoveService;
import com.adcc.loadsheet.billing.util.RestClient;
import com.adcc.loadsheet.billing.vo.ResultStatus;
import com.adcc.loadsheet.billing.vo.ResultVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by AN on 2020-05-27.
 */
@Controller
public class BillingDataMoveController {

    @Autowired
    private SystemConfig systemConfig;

    @Autowired
    private BillingDataMoveService billingDataMoveService;

    //跳转数据同步页面
    @PreAuthorize("hasPermission('','BillingDataMove')")
    @RequestMapping(value = "/billingdatamove", method = RequestMethod.GET)
    public String toBillingDataMove(Model model) {
        return "datamove/datamove";
    }

    @RequestMapping(value = "/billingdatamove/movebase", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO moveBase() {
        ResultVO vo = new ResultVO();
        try {
            String url = systemConfig.getBaseURL();
            String response = RestClient.post(url, "");
            if (!Strings.isNullOrEmpty(response)) {
                vo = JSON.parseObject(response, ResultVO.class);
            } else {
                throw new Exception("request " + url + " failed");
            }
        } catch (Exception ex) {
            vo.setStatus(ResultStatus.FAILED);
            vo.setErrorReason(String.format(ResultVO.ER_CALL_EX_FAILED, "move Base data err", ex.fillInStackTrace()));
        }
        return vo;
    }

    @RequestMapping(value = "/billingdatamove/movelsandps", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO moveLSandPS(@RequestBody Map<String, Object> map) {
        ResultVO vo = new ResultVO();
        try {
            String startDate = map.get("startDate").toString();
            String endDate = map.get("endDate").toString();
            DateTime end=DateTime.parse(endDate);
            String url = systemConfig.getLsandpsURL() + "/" + startDate + "/" + end.plusDays(1).toString("yyyy-MM-dd");
            String response = RestClient.post(url, "");
            if (!Strings.isNullOrEmpty(response)) {
                vo = JSON.parseObject(response, ResultVO.class);
            } else {
                throw new Exception("request " + url + " failed");
            }
        } catch (Exception ex) {
            vo.setStatus(ResultStatus.FAILED);
            vo.setErrorReason(String.format(ResultVO.ER_CALL_EX_FAILED, "move Base data err", ex.fillInStackTrace()));
        }
        return vo;
    }

    @RequestMapping(value = "/billingdatamove/showtips", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO tips() {
        ResultVO vo = new ResultVO();
        try {
            Map<String, Object> data= billingDataMoveService.getTips();
            vo.setData(data);
        } catch (Exception ex) {
            vo.setStatus(ResultStatus.FAILED);
            vo.setErrorReason(String.format(ResultVO.ER_CALL_EX_FAILED, "move Base data err", ex.fillInStackTrace()));
        }
        return vo;
    }



}
